package com.ruoyi.project.user.ipcountrydistribute.controller;

import com.alibaba.druid.util.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.user.ipcountrydistribute.domain.IpCountryDistribute;
import com.ruoyi.project.user.ipcountrydistribute.service.IIpCountryDistributeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户国家分布Controller
 *
 * @author shixiangwen
 * @date 2021-09-08
 */
@Controller
@RequestMapping("/user/ipcountrydistribute")
public class IpCountryDistributeController extends BaseController {
    private String prefix = "user/ipcountrydistribute";

    @Autowired
    private IIpCountryDistributeService ipCountryDistributeService;

    @RequiresPermissions("user:ipcountrydistribute:view")
    @GetMapping()
    public String ipcountrydistribute(ModelMap mmap) {

        IpCountryDistribute ipCountryDistribute = new IpCountryDistribute();
        ipCountryDistribute.setDate(DateUtils.getDate());
        List<IpCountryDistribute> list = ipCountryDistributeService.selectIpCountryDistributeList(ipCountryDistribute);
        List<Map<String, Object>> datas = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> map = new HashMap<>();
            if (!StringUtils.equalsIgnoreCase("中国", item.getCountry())) {
                map.put("name", item.getCountry());
                map.put("value", item.getTotal());
                datas.add(map);
            }
        });
        mmap.put("datas", datas);

        return prefix + "/ipcountrydistribute";
    }

    /**
     * 查询用户国家分布列表
     */
    @RequiresPermissions("user:ipcountrydistribute:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IpCountryDistribute ipCountryDistribute) {
        startPage();
        List<IpCountryDistribute> list = ipCountryDistributeService.selectIpCountryDistributeList(ipCountryDistribute);
        return getDataTable(list);
    }

    /**
     * 导出用户国家分布列表
     */
    @RequiresPermissions("user:ipcountrydistribute:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IpCountryDistribute ipCountryDistribute) {
        List<IpCountryDistribute> list = ipCountryDistributeService.selectIpCountryDistributeList(ipCountryDistribute);
        ExcelUtil<IpCountryDistribute> util = new ExcelUtil<IpCountryDistribute>(IpCountryDistribute.class);
        return util.exportExcel(list, "ipcountrydistribute");
    }

    /**
     * 新增用户国家分布
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户国家分布
     */
    @RequiresPermissions("user:ipcountrydistribute:add")
    @Log(title = "用户国家分布", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IpCountryDistribute ipCountryDistribute) {
        return toAjax(ipCountryDistributeService.insertIpCountryDistribute(ipCountryDistribute));
    }

    /**
     * 修改用户国家分布
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        IpCountryDistribute ipCountryDistribute = ipCountryDistributeService.selectIpCountryDistributeById(id);
        mmap.put("ipCountryDistribute", ipCountryDistribute);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户国家分布
     */
    @RequiresPermissions("user:ipcountrydistribute:edit")
    @Log(title = "用户国家分布", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IpCountryDistribute ipCountryDistribute) {
        return toAjax(ipCountryDistributeService.updateIpCountryDistribute(ipCountryDistribute));
    }

    /**
     * 删除用户国家分布
     */
    @RequiresPermissions("user:ipcountrydistribute:remove")
    @Log(title = "用户国家分布", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(ipCountryDistributeService.deleteIpCountryDistributeByIds(ids));
    }
}
